package com.qzsoft.zhjd.machine.modules.machinerepair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.zhjd.common.components.BaseServiceComponent;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.machine.modules.machineinfo.entity.MachineInfoEntity;
import com.qzsoft.zhjd.machine.modules.machineinfo.service.MachineInfoService;
import com.qzsoft.zhjd.machine.modules.machinerepair.dto.MachineRepairDto;
import com.qzsoft.zhjd.machine.modules.machinerepair.entity.MachineRepairImgEntity;
import com.qzsoft.zhjd.machine.modules.machinerepair.entity.MachineRepairEntity;
import com.qzsoft.zhjd.machine.modules.machinerepair.mapper.MachineRepairMapper;
import com.qzsoft.zhjd.machine.modules.machinerepair.service.MachineRepairImgService;
import com.qzsoft.zhjd.machine.modules.machinerepair.service.MachineRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author HuXiuMing
 * @date 2021/10/21 16:22 维修接口实现
 */

@Service
public class MachineRepairServiceImpl extends ServiceImpl<MachineRepairMapper, MachineRepairEntity>
        implements MachineRepairService {

    @Autowired
    private BaseServiceComponent baseServiceComponent;

    @Autowired
    private MachineRepairImgService machineRepairImgService;

    @Autowired
    private MachineRepairMapper machineRepairMapper;

    @Autowired
    private MachineInfoService machineInfoService;

    /**
     * @param params 查询条件
     * @return 维修分页列表
     * @Title pageDto
     * @Description 获取维修分页列表
     */
    @Override
    public IPage<MachineRepairDto> pageDto(Map<String, Object> params) {
        IPage<MachineRepairDto> page = baseServiceComponent.getPage(params, MachineRepairDto.class);
        page = baseMapper.queryDto(page, params);
        return ConvertDictUtils.formatDicDataPage(page);
    }

    @Override
    public IPage<MachineRepairEntity> page(Map<String, Object> params) {
        // 分页条件
        IPage<MachineRepairEntity> page = baseServiceComponent.getPage(params, MachineRepairEntity.class);

        // 查询条件
        QueryWrapper<MachineRepairEntity> queryWrapper = new QueryWrapper<MachineRepairEntity>();
        if (ObjectUtils.isNotEmpty(params.get("machineId"))) {
            queryWrapper.eq("machine_id", params.get("machineId"));
        }
        if (ObjectUtils.isNotEmpty(params.get("startTime"))) {
            queryWrapper.ge("day", params.get("startTime"));
        }
        if (ObjectUtils.isNotEmpty(params.get("endTime"))) {
            queryWrapper.le("day", params.get("endTime"));
        }
        queryWrapper.orderByDesc("day");
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public MachineRepairEntity getById(Serializable id) {
        // 获取维修信息
        MachineRepairEntity entity = baseMapper.selectById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "维修信息不存在");
        }

        // 获取维修图片
        QueryWrapper<MachineRepairImgEntity> queryWrapper = new QueryWrapper<MachineRepairImgEntity>();
        queryWrapper.eq("repair_id", entity.getId());
        List<MachineRepairImgEntity> imageList = machineRepairImgService.list(queryWrapper);
        entity.setImageList(imageList);

        // 返回结果
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(MachineRepairEntity entity) {
        // 获取设备信息
        MachineInfoEntity machineInfo = machineInfoService.getById(entity.getMachineId());
        if (null == machineInfo) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "机械设备不存在");
        }

        // 保存维修信息
        entity.setDeptPkid(machineInfo.getDeptPkid());
        entity.setDeptId(machineInfo.getDeptId());
        baseMapper.insert(entity);

        // 保存图片信息
        String reapirId = entity.getId();
        List<MachineRepairImgEntity> imageList = entity.getImageList();
        if (ObjectUtils.isNotNull(imageList)) {
            imageList.forEach(item -> {
                item.setRepairId(reapirId);
            });
            machineRepairImgService.saveBatch(imageList);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(MachineRepairEntity entity) {
        // 修改维修信息
        baseMapper.updateById(entity);

        // 删除旧的图片
        QueryWrapper<MachineRepairImgEntity> queryWrapper = new QueryWrapper<MachineRepairImgEntity>();
        queryWrapper.eq("repair_id", entity.getId());
        machineRepairImgService.remove(queryWrapper);

        // 添加新的图片
        String reapirId = entity.getId();
        List<MachineRepairImgEntity> imageList = entity.getImageList();
        if (ObjectUtils.isNotNull(imageList)) {
            imageList.forEach(item -> {
                item.setRepairId(reapirId);
            });
            machineRepairImgService.saveBatch(imageList);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        // 删除维修信息
        machineRepairMapper.deleteById(id);

        // 删除图片信息
        QueryWrapper<MachineRepairImgEntity> queryWrapper = new QueryWrapper<MachineRepairImgEntity>();
        queryWrapper.eq("repair_id", id);
        machineRepairImgService.remove(queryWrapper);
        return true;
    }

}
